<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">

<head>
    <th:block th:include="common/common_head :: commonHeader('预制发票')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">
    <!-- <style type="text/css">.table {table-layout:fixed;}</style> -->
</head>

<body class="gray-bg">
<div class="container-div ui-layout-center">
    <div class="row">
        <div class="col-sm-12 search-condition">
            <form id="invoiceHeaderForm" th:action="@{/invoice/list}" class="form-inline">
                <div class="select-list">
                    <ul>
                        <li class="col-sm-3" style="width: 33%">
                            <label style="width: 120px">发票类型：</label>
                            <select name="invoiceType" sysDict="FPLX" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label style="width: 120px">预制发票号：</label>
                            <input type="text" name="invoiceNo"/>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label style="width: 120px">对账单号：</label>
                            <input type="text" name="billNo"/>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label style="width: 120px">供应商编码：</label>
                            <input type="text" name="invoicePerson"/>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label style="width: 120px">处理状态：</label>
                            <select name="dealStatus">
                                <option value=""></option>
                                <option value="0">未审核</option>
                                <option value="-1">审核驳回</option>
                                <option value="1">审核通过</option>
                                <option value="2">已开票</option>
                            </select>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label style="width: 120px">采购组：</label>
                            <select name="purchGroup" sysDict="CGZ" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label style="width: 120px">SAP发票号：</label>
                            <input name="kjpzbh" type="text">
                        </li>
                        <!--
                        <li class="col-sm-3" style="width: 33%">
                        <label style="width: 120px">物料编码：</label>
                            <input type="text" name="materialCode" />
                        </li>
                         -->
                        <li class="col-sm-3" style="width: 33%">
                            <button id="invoiceHeaderSearchBtn" type="button"
                                    class="btn btn-primary btn-rounded btn-sm"><i class="fa fa-search"></i>&nbsp;搜索
                            </button>
                            <button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置
                            </button>
                            <button id="invoiceHeaderAdvancedSearch" type="button"
                                    class="btn btn-primary btn-rounded btn-sm" style="display: none"><i
                                    class="fa fa-caret-down"></i>&nbsp;高级搜索
                            </button>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="col-sm-12 select-info order-table">
            <div class="btn-group hidden-xs toolbar" id="invoiceHeaderToolBar" role="group">
                <a class="btn btn-outline btn-success btn-rounded" onclick="agreeInvoice(true)">
                    <i class="fa fa-check"></i> 同意
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="agreeInvoice(false)">
                    <i class="fa fa-times"></i> 驳回
                </a>
            </div>
            <div style="height:360px; overflow:auto;">
                <table id="invoiceHeader" data-mobile-responsive="true"></table>
            </div>
            <div>
                <table id="bills" data-mobile-responsive="true"></table>
            </div>
        </div>
    </div>
</div>

<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<script type="text/javascript">
    $(function () {
        initDictDataSel();
    });

    //面向对象
    var headerBS = Searcher.createBS('#invoiceHeader',
        [
            {radio: true},
            {
                title: '处理状态', field: 'dealStatus', formatter: function (value, row, index) {
                    switch (value) {//未操作0，通过1，未通过-1，开票成功2，开票失败-2
                        case 0:
                            return "未审核";
                        case -1:
                            return "审核未通过";
                        case 1:
                            return "审核通过";
                        case 2:
                            return "开票成功";
                        case -2:
                            return "开票失败";
                        default:
                            return value;
                    }
                }
            },
            {title: "发票类型", field: "invoiceType"},
            {title: "预制发票号", field: "invoiceNo"},
            {title: "SAP发票号", field: "kjpzbh"},
            {title: "对账单号", field: "billNo"},
            {title: '发票方', field: 'vendorName'},
            {title: "开票日期", field: "dateKp"},
            {title: "付款日期", field: "dateFk"},
            {title: "过账日期", field: "dateGz"},
            {title: '付款条件', field: 'payCondition'},
            {title: '发票总额', field: 'sumMoney'},
            {title: '凭证抬头文本', field: 'bkTxt'},

            {title: '税码', field:'taxCode'},
            {title: '税额', field:'taxMoney'},

            {title: '货币码', field: 'currency'},
            {title: '备注', field: 'remark'},
            {title: "创建时间", field: "creationDate"},
            {title: "创建人", field: "createBy"}

        ], {
            "pageList": [5,15,50], "pageSize": 5, "showExport": true,
            onCheck: function (row) {
                itemBS.getData({'url': ctxPath + '/scmBills/list?billNo=' + row.billNo});
            },
            onPostBody: function () {
                $("#bills").bootstrapTable('removeAll');
            }
        }
    );

    //刷新数据
    function refreshHeader() {
        headerBS.refresh();
        $("#bills").bootstrapTable('removeAll');
    }

    var itemBS = Searcher.createBS('#bills',
        [
            {title: '采购凭证号', field: 'poNo'},
            {title: '订单行项目号', field: 'itemNo'},
            {title: '采购单类型', field: 'poType'},
            {title: '物料编码', field: 'materialCode'},
            {title: '物料描述', field: 'materialDesc'},
            {title: '工厂', field: 'factory'},
            {title: '库存地点', field: 'storageLocation'},
            {title: '物料组', field: 'materialGroup'},
            {title: '需求者姓名', field: 'requester'},
            {title: '采购订单数量', field: 'poCount'},
            {title: '采购订单的计量单位', field: 'poUnit'},
            {title: '含税单价', field: 'poPrice'},
            {title: '价格单位', field: 'priceUnit'},
            {title: '税码', field: 'taxCode'},
            {title: '订单总价值', field: 'totalPrice'},
            {title: '税额', field: 'taxMoney'},
            {title: '收货日期', field: 'receiptDate'},
            {title: '收货数量', field: 'shippedQty'},
            {title: '成本中心', field: 'costCenter'},
            {title: '订单类型', field: 'poType'},
            {title: '公司代码', field: 'companyCode'},
            {title: '采购组', field: 'purchGroup'},
            {title: '采购组织', field: 'purchOrg'},
            {title: '付款条件代码', field: 'payCondition'},
            {title: '货币码', field: 'currencyCode'},
            {title: '供应商', field: 'vendorName'},
            {title: '供应商建议交期', field: 'suggestDate'},
            {title: '创建人', field: 'createPeople'},
            {title: '创建时间', field: 'createTime'},
            {title: '更新人', field: 'updatePeople'},
            {title: '更新时间', field: 'updateTime'}
        ], {
            "showColumns": false,
            "showRefresh": false,
            "showExport": true,
            "firstLoad": false,
            "pageList": [5, 10, 15],
            "pageSize": 5
        }
    );

    //操作预制发票（同意或者驳回）
    function agreeInvoice(agree) {
        var rowDatas = headerBS.getSelectedRows("pkId");
        if (rowDatas.length <= 0) {
            Dialog.errorMsg("请至少选择一行进行操作！");
            return;
        }
        Page.ajaxPOST(ctxPath + "/invoice/checkInvoice", {"pkId": rowDatas[0], "agree": agree}, function (o) {
            if (o.code == 0) {
                Dialog.msg("成功审核预制发票！");
                alert("创建发票成功!发票号为:" + o.result);
            } else {
                Dialog.alert("审核对预制发票失败!" + o.msg);
            }
            refreshHeader();
        }, {"loadingMsg": "正在往服务器提交数据……"});
    }
</script>
</body>
</html>